package com.yikaiye.android.yikaiye.dialog;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.utils.CommonUtil;

public abstract class MyPopupWindow extends PopupWindow {

    private final PopupWindow popupWindow;

    public MyPopupWindow(Context context) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null);
        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        TextView tvCancelOrder = contentView.findViewById(R.id.tv_cancel_order);
        tvCancelOrder.setOnClickListener(v -> {
            if (!CommonUtil.isCanClick()) return;
            cancelOrder();
            popupWindow.dismiss();
        });
    }

    public void showPopupWindow(View view) {
        if (!popupWindow.isShowing()) {
            popupWindow.showAsDropDown(view, 0, -view.getHeight() * 2 - 20);
        }
    }

    /**
     * android7.0
     */
    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    public abstract void cancelOrder();
}
